﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OALPTool
{
    public partial class FrmDgvStySys : Form
    {
        private string p_dgv_fname = "";
        private string CorpNo = "";
        private string UserNo = "";
        DataView dv = new DataView();

        public FrmDgvStySys()
        {
            InitializeComponent();
        }

        public int InitForm(out string strMsg)
        {
            strMsg = "";
            //CorpNo = ISysPlatformFunc.GetCorpNo();
            //UserNo = ISysPlatformFunc.GetUserNo();
            return ReadFromDB( out strMsg);
        }

        private int ReadFromDB( out string strMsg)
        {
            strMsg = "";
            DataSet ds = null;
            string strsql = "";

            int ret = DgvColEdit.ReadDgFuncSort(CorpNo, UserNo, "SAL_YGSX", strsql, out ds, out strMsg);

            if (ret != 0) return ret;

            if (ds == null || ds.Tables.Count == 0)
            {
                TSysPublic.ShowMsg("null");
                strMsg = "获取样式列表为空!";
                return -1;

            }

            TSysPublic.ShowMsg(ds.Tables[0].Rows.Count.ToString());
 
            //DataTable dt = new DataTable();
            //dt.Columns.Add("col1", typeof(String));
            //dt.Columns.Add("col2", typeof(String));
            //dt.Columns.Add("col3", typeof(String));
            //dt.Columns.Add("col4", typeof(String));
            //dt.Columns.Add("col5", typeof(String));
            //dt.Columns.Add("col6", typeof(String));
            //dt.Columns.Add("col7", typeof(String));
            //dt.Columns.Add("col8", typeof(String));
            //dt.Columns.Add("col9", typeof(String));
            //dt.Columns.Add("col10", typeof(String));
            //dt.Columns.Add("col11", typeof(String));
            //dt.Columns.Add("col12", typeof(String));
            //dt.Columns.Add("col13", typeof(String));
            //dt.Columns.Add("col14", typeof(String));
            //dt.Columns.Add("col15", typeof(String));

            //ds = new DataSet();
            //ds.Tables.Add(dt);
            dv = ds.Tables[0].DefaultView;
     //       dv.Sort = "";

            this.SetDgStyle();
            return 0;
        }

        private void SetDgStyle()
        {

            //DataView dv = new DataView();

            //DataTable dt = new DataTable();
            //dt.Columns.Add("col1", typeof(String));
            //dt.Columns.Add("col2", typeof(String));
            //dt.Columns.Add("col3", typeof(String));
            //dt.Columns.Add("col4", typeof(String));
            //dt.Columns.Add("col5", typeof(String));
            //dt.Columns.Add("col6", typeof(String));
            //dt.Columns.Add("col7", typeof(String));
            //dt.Columns.Add("col8", typeof(String));
            //dt.Columns.Add("col9", typeof(String));
            //dt.Columns.Add("col10", typeof(String));
            //dt.Columns.Add("col11", typeof(String));
            //dt.Columns.Add("col12", typeof(String));
            //dt.Columns.Add("col13", typeof(String));
            //dt.Columns.Add("col14", typeof(String));
            //dt.Columns.Add("col15", typeof(String));

            //dv = dt.DefaultView;
            DataGridViewFunc.setDgvStyle(this.getDgvStyle(), ref this.dgv, dv);

            this.dgv.ReadOnly = true;
            this.dgv.AutoGenerateColumns = false;
            this.dgv.ShowCellToolTips = true;
            this.dgv.AllowUserToAddRows = false;
            this.dgv.AllowUserToDeleteRows = false;
            this.dgv.AllowUserToOrderColumns = false;
            this.dgv.ColumnHeadersHeight = 25;
            this.dgv.RowTemplate.Height = 23;
            this.dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dgv.MultiSelect = false;
        }

        private ArrayList getDgvStyle() 
        {
            ArrayList colList = new ArrayList();
            colList.Add(new object[] { "字段中文名", "FieldNameCh", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "无", null, 1, "", "", "", "", "" });
            //colList.Add(new object[] { "字段英文名", "FieldName", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 2, "", "", "", "", "" });
            colList.Add(new object[] { "显示标志", "ViewFlag", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 3, "", "", "", "", "" });
            colList.Add(new object[] { "显示序号", "ViewSeqNo", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 4, "", "", "", "", "" });
            //colList.Add(new object[] { "排序序号", "SortNo", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "无", null, 5, "", "", "", "", "" });
            //colList.Add(new object[] { "排序方式", "SortFlag", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 6, "", "", "", "", "" });
            //colList.Add(new object[] { "控件类型", "CtrlType", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 7, "", "", "", "", "" });
            colList.Add(new object[] { "显示宽度", "ViewWidth", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 8, "", "", "", "", "" });
            //colList.Add(new object[] { "显示格式化", "ViewFormat", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "无", null, 9, "", "", "", "", "" });
            colList.Add(new object[] { "对齐方式", "ViewAlign", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 10, "", "", "", "", "" });
            //colList.Add(new object[] { "能否修改", "EditAble", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 11, "", "", "", "", "" });
            //colList.Add(new object[] { "数据为空时显示", "nullText", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 12, "", "", "", "", "" });

            return colList;
        }

        private void tbbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tbbAdd_Click(object sender, EventArgs e)
        {
            this.OperateList("0");
        }


        /// <summary>
        /// 列表操作
        /// </summary>
        /// <param name="editmode"></param>
        private void OperateList(string editmode)
        {
            DataTable dt = new DataTable();
            dt = dv.Table.Copy();
            string strMsg = "";
            int ret = 0;
            if (editmode == "1" || editmode == "3")
            {
                if (this.dgv.Rows.Count == 0 || this.dgv.CurrentRow == null) return;
            }
            if (editmode == "0" || editmode == "1")//新增，修改
            {
                DataRow dr = null;
                if (editmode == "0")
                {
                    dr = dt.NewRow();
     //               dr["JS"] = dt.Rows.Count + 1;
                }
                else dr = dt.Rows[this.dgv.CurrentCell.RowIndex];

            }
            else if (editmode == "3")//删除
            {
                int iIndex = this.dgv.CurrentCell.RowIndex;
                DataRow dr = dt.Rows[iIndex];
                dr.Delete();
                dt.AcceptChanges();

                ret = DgvColEdit.DelDgvCol(dr, out strMsg);
                if (ret != 0)
                {
                    TSysPublic.ShowErrMsg("删除失败!" + strMsg);
                    return;
                }
            }

            if (this.InitForm(out strMsg) < 0)
            {
                TSysPublic.ShowMsg("列表更新失败!" + strMsg);
                return;
            }
        }

        private void tbbSetStyle_Click(object sender, EventArgs e)
        {
            FrmDgvSet Frm = new FrmDgvSet();
            Frm.ShowInTaskbar = false;
            string strMsg = "";
            if (Frm.InitForm(out strMsg) < 0) 
            {
                TSysPublic.ShowMsg("界面初始化失败！");
            }
            Frm.ShowDialog();
            Frm.Dispose();
        }
    }
}
